//
//  Latestlottery.m
//  WangLottery
//
//  Created by leven hu on 12-4-22.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "Latestlottery.h"

@implementation Latestlottery

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"试试手气";
    // Do any additional setup after loading the view from its nib.
    UIBarButtonItem *favBtn=[[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(Favorite)];
    self.navigationItem.rightBarButtonItem=favBtn;
    [favBtn release];
    
    randomArray=[[NSArray alloc]init];
    
    [self selectPicview];
}

- (void)dealloc{

    [randomArray release];
    [sortArray release];
    
    [randomPicker release];
    
    [super dealloc];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewDidAppear:(BOOL)animated{

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma UserFav
//收藏
- (void)Favorite
{
   FavCode *viewController = [[FavCode alloc] initWithNibName:@"FavCode" bundle:nil];
    
   [self.navigationController pushViewController:viewController animated:YES];
    
    
    if (sortArray != nil) {
        
        viewController.codeArray=sortArray;
        
    }
}

- (IBAction)Betting{
 
    ConfirmCode *viewController = [[ConfirmCode alloc] initWithNibName:@"ConfirmCode" bundle:nil];
    
 
    [self.navigationController pushViewController:viewController animated:YES];
    
    if (sortArray != nil) {
        
        viewController.codeArray=sortArray;
        
    }
    
    
    
}

#pragma PickView;

- (void)selectPicview{
    
    NSMutableArray *NUM=[[NSMutableArray alloc] init];
    int a[5];
    
    
    
    //[self selectRandomNum:22];
    
    for (int i=0; i<5; i++) {
    
        BOOL mark=YES;  //标记是否可以加到数组里面
        
        srand((unsigned)time(0)); //种子 
        
        int index = arc4random()%22; //随机数
        
        for (int j=0; j<5; j++) {
            
            if (a[j] == index || index == 0) {
                
                
                i--;
                
                mark=NO;
                
                break;
            }
            
        }

        if (mark) {
            
            a[i]=index;
            
            NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
            [dic setValue:[NSNumber numberWithInt:index] forKey:@"row"];
            [dic setValue:[NSNumber numberWithInt:i] forKey:@"com"];
            
            [NUM addObject:dic];
            
            [dic release];
            
        }
    }
    
   [self selectAnimat:NUM];
    
    [NUM release];
    
}


//选择随机数
- (int)selectRandomNum:(int)num{
    
    srandom(time(NULL));
    
   return  arc4random() % 22;

}

- (void)selectAnimat:(NSArray *)array{
    
    NSMutableArray *rendomcode=[[NSMutableArray alloc]init];
    
    NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"row" ascending:YES]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sorter count:1]; 
    NSArray *sortedArray = [array sortedArrayUsingDescriptors:sortDescriptors];
   
    for (int i=0; i<[sortedArray count]; i++) {

        NSDictionary *dic=[[NSDictionary alloc]initWithDictionary:[sortedArray objectAtIndex:i]];
        
       NSInteger row=[[dic objectForKey:@"row"] intValue];

       [randomPicker selectRow:row inComponent:i animated:YES];
        
        [rendomcode addObject:[NSNumber numberWithInt:row]];
        
       [dic release];
        
    }
    
    srand((unsigned)time(0)); //种子 
    
    int index1 = arc4random() % 7;

    [randomPicker selectRow:index1 inComponent:5 animated:YES];
    
    [rendomcode addObject:[NSNumber numberWithInt:index1]];
    
    sortArray=[[NSArray alloc]initWithArray:rendomcode];
    
    [rendomcode release];
    
    [sortDescriptors release];
    [sorter release];
    
    
}
 

//返回列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 6;
}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    

    NSMutableArray *array=[[NSMutableArray alloc]init ];
;
    if (component != 5) {
    for (int i=1; i<23; i++) {
        [array addObject:[NSNumber numberWithInt:i]];
    }
    
    return [NSString stringWithFormat:@"%d",[[array objectAtIndex:row] intValue]];
        
    }
    else {
    
        for (int i=1; i<8; i++) {
            [array addObject:[NSNumber numberWithInt:i]];
        }
        
        return [NSString stringWithFormat:@"0%d",[[array objectAtIndex:row] intValue]];
        
    }
    
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    if (component != 5) {
        
         return 22; 
    }
    else 
        return 7;
    
}


@end
